﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyTestABP.Entities;
using Abp.Application.Services;
using MyTestABP.Students.Dto;
using MyTestABP.Student;


namespace MyTestABP.Classes
{
    public class StudentAppService : IStudentAppService
    {
        private readonly IRepository<MyTestABP.Entities.Student> _studentRepository;

        public StudentAppService(IRepository<MyTestABP.Entities.Student> StudentRepository)
        {
            _studentRepository = StudentRepository;
        }

        //统计每个班级下有多少学生
        //public List<ClassOutput> GetClassSummry() {

        //    List<ClassOutput> list = new List<ClassOutput>();
        //    //-_classRepository.
        //    var data = _classRepository.GetAll().Select(x => new ClassOutput
        //    {
        //        Name=x.name,
        //        number=x.Students.Count()
        //    }).ToList();
        //    return data;
        //}

        public List<StudentOutput> GetClassSummry()//获取班级学生人数信息
        {
            //-_classRepository.
            var data = _studentRepository.GetAll().Select(x => new
            {
                x.name,
                className = x.classes.name,
                id = x.classesId
            }).GroupBy(x => new { x.id, x.className }).Select(x => new StudentOutput
            {
                id = x.Key.id,
                className = x.Key.className,
                count = x.Count()
            }).ToList();
            return data;
        }
        public List<StudentDto> GetStudents()
        {
            var data = _studentRepository.GetAll().Select(x => new StudentDto
            {
                id = x.Id,
                name = x.name,
                cid = x.classesId,
                className = x.classes.name
            }).ToList();
            return data;
        }
        public int addStudent(StudentInput s)
        {
            if (_studentRepository.Count(x => x.name == s.name) > 0)
            {
                return -1;
            }
           MyTestABP.Entities.Student _student=new MyTestABP.Entities.Student();
           _student.name = s.name;
           _student.classesId = s.cid;
           //_student.classes.Id = s.cid;
           //_student.classes.name = s.classname;
           _studentRepository.InsertAndGetId(_student);
           return 1;
        }
        public int updateStudent(StudentInput s)
        {
            MyTestABP.Entities.Student _student = _studentRepository.GetAll().Where(x=>x.Id==s.id).FirstOrDefault();
            _student.name = s.name;
            _student.classesId = s.cid;

            _studentRepository.Update(_student);
            return 1;
        }
        public int deleteStudent(StudentInput s)
        {
            MyTestABP.Entities.Student _student = _studentRepository.GetAll().Where(x => x.Id == s.id).FirstOrDefault();
            _student.name = s.name;
            _student.classesId = s.cid;
            _studentRepository.Delete(_student);
            return 1;
        }
    }
}
